package com.woniuxy.controller;

import com.woniuxy.pojo.InspectionRecord;
import com.woniuxy.service.InspectionRecordService;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import com.woniuxy.vo.InspectionRecordVo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class InspectionRecordController extends BaseController{
    private Logger logger= LoggerFactory.getLogger(this.getClass());

    @Autowired
    private InspectionRecordService inspectionRecordService;

    @ApiOperation(value = "查找年检记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "查找年检记录")
    @GetMapping("/inspectionRecords")
    public DataGridView findInspectionRecordsByPage(InspectionRecordVo inspectionRecordVo){
        logger.debug("年检记录："+inspectionRecordVo.toString());
        return inspectionRecordService.findByPage(inspectionRecordVo);
    }

    @ApiOperation(value = "删除年检记录",httpMethod = "DELETE",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "删除年检记录")
    @DeleteMapping(value = "/inspectionRecords/{id}")
    public ResultDto removeInspectionRecord(@PathVariable("id") int id){
        try {
            inspectionRecordService.removeById(id);
            return ResultDto.INSPECTION_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.INSPECTION_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "批量删除年检记录",httpMethod = "GET",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "批量删除年检记录")
    @GetMapping(value = "inspectionRecords/deleteBatch")
    public ResultDto removeBathInspectionRecords(@RequestParam("ids[]") Integer[] ids){
        try {
            inspectionRecordService.removeBatch(ids);
            return ResultDto.INSPECTION_RECORD_DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.INSPECTION_RECORD_DELETE_SUCCESS;
        }
    }

    @ApiOperation(value = "添加年检记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "添加年检记录")
    @PostMapping(value = "/inspectionRecords")
    public ResultDto addInspectionRecord(InspectionRecord inspectionRecord){
        try {
            boolean flag=inspectionRecordService.add(inspectionRecord);
            if (flag) {
                return ResultDto.INSPECTION_RECORD_ADD_SUCCESS;
            } else {
                return ResultDto.INSPECTION_RECORD_ADD_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.INSPECTION_RECORD_ADD_FAIL;
        }
    }

    @ApiOperation(value = "编辑年检记录",httpMethod = "POST",
            protocols = "HTTP",produces = "application/json",
            response = ResultDto.class,notes = "编辑年检记录")
    @PostMapping(value = "/inspectionRecords.do")
    public ResultDto updateInspectionRecord(InspectionRecord inspectionRecord){
        try {
            boolean flag=inspectionRecordService.update(inspectionRecord);
            if (flag) {
                return ResultDto.INSPECTION_RECORD_UPDATE_SUCCESS;
            } else {
                return ResultDto.INSPECTION_RECORD_UPDATE_FAIL;
            }
        } catch (Exception e){
            e.printStackTrace();
            return ResultDto.INSPECTION_RECORD_UPDATE_FAIL;
        }
    }
}
